package com.smart.upms.sys.controller;


import com.smart.core.starter.base.BaseController;
import com.smart.core.starter.model.R;
import com.smart.upms.sys.param.SysMenuParam;
import com.smart.upms.sys.result.SysMenuResult;
import com.smart.upms.sys.result.SysMenuTreeResult;
import com.smart.upms.sys.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限 前端控制器
 * </p>
 *
 * @author guxiaobai
 * @since 2018-12-05
 */
@Api(description = "权限管理接口")
@RestController
@RequestMapping("/sys/sys-menu")
@AllArgsConstructor
public class SysMenuController extends BaseController {
    private final ISysMenuService sysMenuService;

    /**
     * 添加菜单
     *
     * @param param 菜单信息
     * @return success/false
     */
    @ApiOperation(value = "添加菜单")
    @PostMapping
    public R<Boolean> add(@Validated @RequestBody SysMenuParam param) {
        return R.success(sysMenuService.save(param));
    }


    /**
     * 更新菜单
     *
     * @param param 菜单信息
     * @return
     */
    @ApiOperation(value = "更新菜单")
    @PutMapping
    public R<Boolean> update(@Validated @RequestBody SysMenuParam param) {
        return R.success(sysMenuService.update(param));
    }

    /**
     * 删除菜单
     *
     * @param id 菜单ID
     * @return success/false
     */
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        return R.success(sysMenuService.delete(id));
    }


    /**
     * 通过ID查询
     *
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @ApiOperation(value = "通过ID查询")
    @GetMapping("/{id}")
    public R<SysMenuResult> get(@PathVariable Long id) {
        return R.success(sysMenuService.get(id));
    }

    /**
     * 通过角色编码查询菜单
     *
     * @param roleCode 角色编码
     * @return 菜单列表
     */
    @ApiOperation(value = "通过角色编码查询菜单")
    @GetMapping("/findByRole/{roleCode}")
    public R<List<SysMenuResult>> findMenuByRole(@PathVariable String roleCode) {
        return R.success(sysMenuService.findByRoleCode(roleCode));
    }

    /**
     * 查询用户的权限
     *
     * @return 当前用户的树形菜单
     */
    @ApiOperation(value = "查询用户的权限")
    @GetMapping(value = "/userMenu/{userId}")
    public R<List<SysMenuTreeResult>> userMenu(@PathVariable Long userId) {
        return R.success(sysMenuService.userMenu(userId));
    }

    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @ApiOperation(value = "返回树形菜单集合")
    @GetMapping(value = "/allTree")
    public R<List<SysMenuTreeResult>> allTree() {
        return R.success(sysMenuService.allTree());

    }

    /**
     * 通过角色编码查询菜单id集合
     *
     * @param roleCode 角色编码
     * @return 属性集合
     */
    @ApiOperation(value = "通过角色编码查询菜单id集合")
    @GetMapping("/roleTree/{roleCode}")
    public R<List<Long>> findMenuIdsByRole(@PathVariable String roleCode) {
        List<SysMenuResult> sysMenuResults = sysMenuService.findByRoleCode(roleCode);
        return R.success(sysMenuResults.stream().map(SysMenuResult::getId).collect(Collectors.toList()));
    }





}
